{% extends 'base_user_center.html' %}
{% load staticfiles %}
{% block user_right_content %}
    <div class="right_content clearfix">
        <h3 class="common_title2">收货地址</h3>
        <div class="site_con">
            <dl>
                <dt>当前地址：</dt>
                {% if not addresses %}
                    <dd>目前还没有添加地址</dd>
                {% else %}
                    {% for address in addresses %}
                        <dd><input type="radio" name="address-choice" value="{{ address.id }}" {% if address.is_default %}checked{% endif %}>{{ address.address }}（{{ address.receiver }}收）{{ address.phone_num }}</dd>
                    {% endfor %}
                {% endif %}
            </dl>
        </div>
        <h3 class="common_title2">编辑地址</h3>
        <div class="site_con">
            <form method="post">
                {% csrf_token %}
                <div class="form_group">
                    <label>收件人：</label>
                    <input type="text" name="receiver">
                </div>
                <div class="form_group form_group2">
                    <label>详细地址：</label>
                    <textarea class="site_area" name="address"></textarea>
                </div>
                <div class="form_group">
                    <label>邮编：</label>
                    <input type="text" name="post_code">
                </div>
                <div class="form_group">
                    <label>手机：</label>
                    <input type="text" name="phone">
                </div>

                <input type="submit" name="" value="提交" class="info_submit">
            </form>
            {{ errormessage }}
        </div>
    </div>
{% endblock user_right_content %}

{% block bottomfiles %}
    <script type="text/javascript" src="{% static 'js/jquery-1.12.4.min.js' %}"></script>
    <script type="text/javascript">
        $(function(){
            var default_id = $("input[name='address-choice']:checked").val()
            $("input[name='address-choice']").click(function(){
                var choice_id = $(this).val()
                if(default_id !== choice_id)
                {
                    // 把choice_id传到后台，进行修改
                    $.get({
                        url:'/user/defaultaddrchange',
                        async:true,
                        data:{'default_id':choice_id},
                        success(res){
                            if(res.code === 200)
                            {
                                default_id = choice_id
                            }
                        },
                        fail(err){
                            return false
                        }
                    })
                }
            })
        })
    </script>
{% endblock bottomfiles %}